package com.niuxiaofa.newlaw.Activity;


import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.MemberFragmentInfoBean;
import com.niuxiaofa.newlaw.Network.ApiService;
import com.niuxiaofa.newlaw.Network.ServiceManager;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import www.linwg.org.lib.LCardView;

/**
 * 我的仓库
 */
public class MyWarehouseActivity extends BaseActivity {


    @BindView(R.id.progress1)
    View progress1;

    @BindView(R.id.progress2)
    View progress2;

    @BindView(R.id.progress3)
    View progress3;


    @BindView(R.id.memberHas1)
    TextView memberHas1;

    @BindView(R.id.memberHas2)
    TextView memberHas2;

    @BindView(R.id.memberHas3)
    TextView memberHas3;


    @BindView(R.id.jilu)
    LCardView jilu;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_warehouse;
    }

    @Override
    protected void initData() {

        setTitle("碎片记录");
        jilu.setOnClickListener(e -> {
            ToActivityUtil.toNextActivity(BlindBoxRecordActivity.class);
        });


        Call<MemberFragmentInfoBean> call = ServiceManager.getInstance().creat(ApiService.class).getMemberFragmentInfo();
        call.enqueue(new Callback<MemberFragmentInfoBean>() {
            @Override
            public void onResponse(Call<MemberFragmentInfoBean> call, Response<MemberFragmentInfoBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    MemberFragmentInfoBean bean = (MemberFragmentInfoBean) response.body();


                    memberHas1.setText("(" + bean.getData().get(0).getMemberHas() + "/" + bean.getData().get(0).getFragmentTotal() + ")");
                    memberHas2.setText("(" + bean.getData().get(1).getMemberHas() + "/" + bean.getData().get(1).getFragmentTotal() + ")");
                    memberHas3.setText("(" + bean.getData().get(2).getMemberHas() + "/" + bean.getData().get(2).getFragmentTotal() + ")");


                    setProgress(progress1, bean.getData().get(0).getMemberHas(), bean.getData().get(0).getFragmentTotal());
                    setProgress(progress2, bean.getData().get(1).getMemberHas(), bean.getData().get(1).getFragmentTotal());
                    setProgress(progress3, bean.getData().get(2).getMemberHas(), bean.getData().get(2).getFragmentTotal());

                }
            }

            @Override
            public void onFailure(Call<MemberFragmentInfoBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());

            }
        });

    }

    public void setProgress(View view, float progress, int sum) {
        LCardView.LayoutParams linearParams = (LCardView.LayoutParams) view.getLayoutParams();
        linearParams.width = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (120 / sum) * progress, getResources().getDisplayMetrics()));
        view.setLayoutParams(linearParams);

    }


}
